// components/comment/comment.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    movieId: String,
    movietitle: String,
    movieimage: String
  },

  data: {
    isBtn: true,
    comments: [],
    gridCol: 3,
    skin: false,
    content: "",
    text: "",
    mid: ""
  },

  observers: {
    'movieId': function (val) {
      this.getcommentbymovieid()
    }
  },

  methods: {
    // 通过电影id获取评论
    getcommentbymovieid: function () {
      let that = this
      let com = this.data.comments
      const db = wx.cloud.database()
      db.collection('comment').orderBy('date', 'desc').where({
        "movieid": that.properties.movieId,
      }).get().then(res => {
        res.data.map(v => {
          v.date = v.date.slice(5, -1) + " " + v.time
        })
        that.setData({
          comments: res.data
        })
      })
    },
    // 获取时间
    getNowFormatTime: function () {
      var myDate = new Date();
      //获取当前年
      var year = myDate.getFullYear();
      //获取当前月
      var month = myDate.getMonth() + 1;
      //获取当前日
      var date = myDate.getDate();
      var h = myDate.getHours();       //获取当前小时数(0-23)
      var m = myDate.getMinutes();     //获取当前分钟数(0-59)
      if (m < 10) m = '0' + m;
      var s = myDate.getSeconds();
      if (s < 10) s = '0' + s;
      var nowhour = h + ':' + m;
      return nowhour;
    },
    // 获取日期
    getNowFormatDate: function () {
      var myDate = new Date();
      //获取当前年
      var year = myDate.getFullYear();
      //获取当前月
      var month = myDate.getMonth() + 1;
      //获取当前日
      var date = myDate.getDate();
      var h = myDate.getHours();       //获取当前小时数(0-23)
      var m = myDate.getMinutes();     //获取当前分钟数(0-59)
      if (m < 10) m = '0' + m;
      var s = myDate.getSeconds();
      if (s < 10) s = '0' + s;
      var nowtime = year + '-' + month + "-" + date;
      return nowtime;
    },
    // 绑定评论内容
    updateValue: function (e) {
      this.setData({
        content: e.detail.value,
        text: e.detail.value
      })
    },
    // 添加评论
    commentpost: async function (e) {
      this.setData({
        text: "",
      })
      this.setData({
        isBtn: false
      })
      let that = this;
      const db = wx.cloud.database();

      if (!that.data.content) {
        wx.showToast({
          title: '请输入评论的内容!',
          icon: "none"
        })
        that.setData({
          isBtn: true,
          text: "",
          content: ""
        })
        return
      }
      let tempuser = app.globalData.userInfo;
      await db.collection("comment").add({
        data: {
          "movieid": that.properties.movieId,
          "userid": wx.getStorageSync('openid'),
          "avatarUrl": tempuser.avatarUrl,
          "nickName": tempuser.nickName,
          "content": that.data.content,
          "time": that.getNowFormatTime(),
          "date": that.getNowFormatDate(),
          "movieimage": that.properties.movieimage,
          "movietitle": that.properties.movietitle,

        }
      }).then(res => {
        wx.showToast({
          title: '评论成功!',
        })
        that.setData({
          text: "",
          isBtn: true,
          content: ""
        })
        that.getcommentbymovieid()
      }, err => {
        wx.showToast({
          title: '请求失败!',
          icon: "none"
        })
        that.setData({
          text: "",
          isBtn: true,
          content: ""
        })
      })
    }
  }


})
